{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# coding:utf-8\n",
    "import jieba\n",
    "from gensim.models.word2vec import Word2Vec\n",
    "from gensim.models import word2vec\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import multiprocessing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "senteces = word2vec.Text8Corpus('./wiki.seg.txt')\n",
    "model = Word2Vec(senteces,min_count=5,size = 128,window=6,workers=multiprocessing.cpu_count())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/fwj/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: DeprecationWarning: Call to deprecated `most_similar` (Method will be removed in 4.0.0, use self.wv.most_similar() instead).\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[('王', 0.5474553108215332),\n",
       " ('妃', 0.5384432673454285),\n",
       " ('懿', 0.4976070523262024),\n",
       " ('嗣', 0.4868311882019043),\n",
       " ('宗', 0.4819859266281128),\n",
       " ('睿', 0.46555277705192566),\n",
       " ('嫔', 0.4643722474575043),\n",
       " ('愍', 0.4542901813983917),\n",
       " ('恭', 0.4423616826534271),\n",
       " ('庭', 0.4417194128036499)]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.most_similar('皇')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "outp1 = 'wiki.zh.text.model'\n",
    "model.save(outp1)\n",
    "model.wv.save_word2vec_format('char_vector_128.txt', binary=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/fwj/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: DeprecationWarning: Call to deprecated `most_similar` (Method will be removed in 4.0.0, use self.wv.most_similar() instead).\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[('南', 0.8887668251991272),\n",
       " ('东', 0.7735404372215271),\n",
       " ('湾', 0.5367575883865356),\n",
       " ('西', 0.48947325348854065),\n",
       " ('澎', 0.4347734749317169),\n",
       " ('汴', 0.4035177528858185),\n",
       " ('城', 0.39935827255249023),\n",
       " ('滨', 0.3808877170085907),\n",
       " ('畿', 0.3787684440612793),\n",
       " ('陵', 0.3476642668247223)]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.most_similar('北')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/fwj/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: DeprecationWarning: Call to deprecated `__getitem__` (Method will be removed in 4.0.0, use self.wv.__getitem__() instead).\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ -1.03980911e+00,   1.54811025e+00,   1.49984968e+00,\n",
       "         5.26264524e+00,  -5.43332659e-02,   4.29637343e-01,\n",
       "        -2.66074514e+00,   7.15267849e+00,   1.85768127e+00,\n",
       "        -1.32152188e+00,   2.40831947e+00,  -2.90635794e-01,\n",
       "         5.60913038e+00,   1.02041602e+00,   6.92044735e-01,\n",
       "        -2.01043177e+00,  -2.77650309e+00,  -3.03553271e+00,\n",
       "        -1.51679128e-01,   6.30173349e+00,   2.06800729e-01,\n",
       "         1.54193148e-01,   4.84365511e+00,  -7.90696096e+00,\n",
       "        -9.37934339e-01,  -1.92316502e-01,  -5.39744139e+00,\n",
       "        -6.35691452e+00,  -3.73305631e+00,   4.79136610e+00,\n",
       "        -5.43961406e-01,  -2.84795332e+00,  -1.80771482e+00,\n",
       "         5.72693586e-01,   9.90435886e+00,   6.48577881e+00,\n",
       "        -5.33672142e+00,  -8.94535482e-01,  -2.12615466e+00,\n",
       "        -5.98308372e+00,  -2.71465135e+00,  -3.94909739e+00,\n",
       "         3.12359929e+00,   8.17530251e+00,  -6.19534826e+00,\n",
       "        -7.02084243e-01,   5.13005447e+00,  -3.63119984e+00,\n",
       "         5.82360220e+00,  -5.41045237e+00,   2.03259870e-01,\n",
       "        -7.35072851e-01,  -7.78115988e+00,  -3.68945813e+00,\n",
       "        -2.31826282e+00,  -6.33339047e-01,  -1.80997479e+00,\n",
       "        -9.09838438e-01,   6.06612396e+00,   3.08955288e+00,\n",
       "         5.32419014e+00,   3.62511367e-01,  -2.16400623e+00,\n",
       "        -2.63552380e+00,  -5.91660617e-03,  -3.00623965e+00,\n",
       "         1.67486370e+00,   5.75106144e+00,  -1.06296909e+00,\n",
       "         4.57956696e+00,  -8.36739159e+00,   2.02819300e+00,\n",
       "        -3.49064136e+00,  -1.59586394e+00,   3.76986670e+00,\n",
       "        -6.34704256e+00,   1.39965117e+00,  -7.29510546e-01,\n",
       "         6.54864836e+00,  -1.70591235e+00,  -1.55919671e-01,\n",
       "        -3.37508583e+00,   1.76382482e+00,   5.42866945e+00,\n",
       "        -1.53629255e+00,   6.27980769e-01,  -2.64670193e-01,\n",
       "        -3.20911837e+00,  -3.14361167e+00,  -5.98375034e+00,\n",
       "        -6.47278929e+00,  -3.13757014e+00,  -1.38223720e+00,\n",
       "         5.86910152e+00,  -5.64287281e+00,  -5.97567499e-01,\n",
       "         9.77696419e-01,  -1.03185928e+00,   3.62852722e-01,\n",
       "         4.61055851e+00,   3.61505628e+00,  -3.73487473e-01,\n",
       "         3.74243999e+00,   1.43969297e+00,   4.04671526e+00,\n",
       "        -1.11918001e+01,  -1.74034131e+00,  -1.45910752e+00,\n",
       "         2.57900119e+00,   8.89209032e-01,  -2.92228317e+00,\n",
       "        -5.79907894e-01,   6.11610603e+00,  -1.94798577e+00,\n",
       "         6.03281069e+00,  -4.79155493e+00,   4.16437149e-01,\n",
       "        -2.06395888e+00,  -2.77832896e-01,  -7.89708316e-01,\n",
       "         2.30597377e+00,   2.77658463e-01,   1.48574603e+00,\n",
       "         6.32292986e-01,   3.68563771e+00,  -1.53613687e-01,\n",
       "         5.29683208e+00,  -2.36915541e+00], dtype=float32)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model['北']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
